package com.hongxun.net;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.ZipUtils;
import com.hongxun.greenentity.tk.DPictureEntity;
import com.hongxun.interfaces.NovaCallback;
import com.tamic.novate.Novate;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dmax.dialog.SpotsDialog;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by admin on 2018/7/18.
 */

public class BaseNet {

    public static SpotsDialog d;

    /**
     * 获取网络请求实例
     *
     * @param context
     * @return
     */
    protected static Novate getNovate(Context context) {
        return new Novate.Builder(context).baseUrl(Contants.baseUrl).connectTimeout(30).readTimeout(30).addLog(true).addHeader(getHead(context)).build();
    }

    protected static Novate getNovate(Context context, String msg) {
        getDialog(context, msg);
        return getNovate(context);
    }

    protected static SpotsDialog getDialog(Context context, String msg) {
        if (d != null) {
            d.dismiss();
            d.cancel();
            d = null;
        }
        if (msg != null) d = new SpotsDialog(context, msg);
        else d = new SpotsDialog(context);
        d.show();
        d.setCancelable(false);
        return d;
    }

    /**
     * 统一获取请求头部
     *
     * @param context
     * @return
     */
    protected static Map<String, String> getHead(Context context) {
        Map<String, String> headers = new HashMap<>();
        String token = (String) SPUtils.get(context, "token", "");
        if (token != null && !token.equals("")) {
            headers.put("token", token);
        }
        headers.put("Content-Type", "application/json;charset=utf-8");
        headers.put("version", "1.0");
        return headers;
    }

    protected static Map<String, Object> getParams(Context context) {
        Map<String, Object> map = new HashMap<>();
        String token = (String) SPUtils.get(context, "token", "");
        if (!token.equals("")) {
            map.put("token", token);
        }
        return map;
    }

    protected static void upPic(final Context context, final List<DPictureEntity> list, final NovaCallback c, int i) {
        List<DPictureEntity> upList = new ArrayList<>();//每次上传限制10张图片
        List<DPictureEntity> upList2 = new ArrayList<>();//每次上传限制10张图片  11
        for (int j = 0; j < list.size(); j++) {
            DPictureEntity d = list.get(j);
            upList2.add(d);
            upList.add(d);
            if (j == 20) break;
        }
        list.removeAll(upList2);//将已经上传的图片删除
        ZipUtils.zip(upList);//先打包文件
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data; charset=utf-8"), new File(ZipUtils.zipFilePath));
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("fileName", "zip", requestFile)
                .addFormDataPart("photo", JSON.toJSONString(upList))
                .build();
        getNovate(context).rxUploadWithBody("", Contants.photoUpload, requestBody, new Callback(i, new NovaCallback() {
            @Override
            public void success(int i, JSONObject obj) {
                if (list.size() == 0) {
                    d.cancel();
                    c.success(i, null);
                } else {
                    upPic(context, list, c, i);
                }
            }

            @Override
            public void err(int i, int status, String err) {
                d.cancel();
                c.err(i, status, err);
            }
        }, null));
    }

}
